/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Cliente.Controlador;

import Cliente.Modelo.BoardPlaying;
import Cliente.Modelo.Game;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jorge Perez
 */
class WaitForOrders extends Thread {

    private Game game;
    private Socket socket;
    private BufferedReader read;
    private PrintWriter write;

    public WaitForOrders(Game game, Socket socket, BufferedReader read, PrintWriter write) {
        this.game = game;
        this.socket = socket;
        this.read = read;
        this.write = write;
    }

    @Override
    public void run() {
        String order = null;
        do {
            String boardInfo = null;
            try {
                order = read.readLine();
            } catch (IOException ex) {
                System.out.println("Error al leer la operacion");
                try {
                    throw new Exceptions(Exceptions.ERROR_03);
                } catch (Exceptions ex1) {
                    Logger.getLogger(WaitForOrders.class.getName()).log(Level.SEVERE, null, ex1);
                }

            }

            if (order.equals(ConnectionConstraints.GAME_UPDATE)) {
                try {
                    boardInfo = read.readLine();
                } catch (IOException ex) {
                    System.out.println("Error al leer el tablero");
                    try {
                        throw new Exceptions(Exceptions.ERROR_03);
                    } catch (Exceptions ex1) {
                        Logger.getLogger(WaitForOrders.class.getName()).log(Level.SEVERE, null, ex1);
                    }
                }


                StringTokenizer str = new StringTokenizer(boardInfo, "#");
                if (str.countTokens() != 14) {
                    System.out.println("Numero de tokens del boardplaying incorrectos");
                }
                BoardPlaying board = game.getPartida();
                board.setId_game(Integer.parseInt(str.nextToken()));
                board.setPlayer_turn(Integer.parseInt(str.nextToken()));
                board.setRound(Integer.parseInt(str.nextToken()));
                board.setRoll_dice1(Integer.parseInt(str.nextToken()));
                board.setRoll_dice2(Integer.parseInt(str.nextToken()));

                board.setP1_position(Integer.parseInt(str.nextToken()));
                board.setP2_position(Integer.parseInt(str.nextToken()));
                board.setP3_position(Integer.parseInt(str.nextToken()));
                board.setP4_position(Integer.parseInt(str.nextToken()));
                board.setP5_position(Integer.parseInt(str.nextToken()));
                board.setP6_position(Integer.parseInt(str.nextToken()));
                board.setP7_position(Integer.parseInt(str.nextToken()));
                board.setP8_position(Integer.parseInt(str.nextToken()));

                board.setMessage(str.nextToken());

                game.notificar_cambio_tablero(board);
            }

        } while (!order.equals(ConnectionConstraints.GAME_FINISH));

        try {
            socket.close();
            read.close();
        } catch (IOException ex) {
            System.out.println("Fallo al cerrar la conexion");

        }
        write.close();
    }
}
